$(function(){
  
  if ($('.alphabet').size()>0&&document.body.clientHeight<400){
    window.onscroll=function(){
       var alphabet=document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
      if(alphabet>85){
          $('.alphabet').css("top","0");
      }else{
          $('.alphabet').css("top","3.5em");
      }
    };
  };

	$("img.lazy").lazyload({}); 
	
	/*评价验证*/
	$('.product-review-submit').validate();
	
	$('.complain-submit').validate();
	
    $('.inputtest').on('click','.input-key',function(event){
        var circuitcity='.circuitnavigate';
        glideShow(circuitcity);
        minHeight();
        setTimeout(function(){
            $(circuitcity).css("position","absolute");
         },500);
        focusAjaxGetDhSearch()
   	    event.stopPropagation(); 
    });

    $('.circuitheader-position').on('click','.icon-city',function(event){
        var circuitcity='.circuitcity';
        glideShow(circuitcity);    
        event.stopPropagation();   
    });

    $('.circuitn-left').on('click','.circuitn-ieft',function(event){
        glidehide();
        event.stopPropagation();   
    });

    $('.circuitcity').on('click','.nav-dlclick dd',function(event){
        $('.circuitheader-cs').html($(this).html());
        glidehide();
        event.stopPropagation();
    });

    $('.circuitlike-footer').on('click','.circuitlike-ulfooer li',function(event){
    	
        $('.circuitlike-ulfooer li').removeClass("active");
        $(this).addClass("active");
        $('.Zone-one,.Zone-twe,.Zone-three').hide();
        $('.circuittest-like,.circuitheader-heard').hide();
        if($(this).index()==0){
            $('.Zone-one').show();
        }else if($(this).index()==1){
            $('.Zone-twe').show();
        }else if($(this).index()==2){
            $('.Zone-three').show();
        }
        event.stopPropagation();
    })

    $('.circuitheader-li').on('click','.circuitheader-fasle',function(event){
        $('.circuitlike-ulfooer li').removeClass("active");
        $('.Zone-one,.Zone-twe,.Zone-three').hide();
        $('.circuittest-like,.circuitheader-heard').show();
        event.stopPropagation();
    })
    
    $('.circuitcity .nav-dlclick').on('click','dd',function(event){
         $('.icon-city .circuitheader-cs').text($(this).text())
         $('html,body').animate({scrollTop:'3em'},"normal");
         glidehide();
    	 event.stopPropagation();
    })
    
    /*  首页点击  出游类型，目的地搜索 */
    $('.common-href').on('click','li a',function(event){
         var href = $(this).attr('href');
         var start_address = $('.circuitheader-cs').text();
         href = href+'&start_address='+start_address;
         location.href = href;
         event.preventDefault(); 
    })
    
    $('.circuithui').on('click','.show-review',function(event){
        var circuitcity='.circuitnavigate';
        glideShow(circuitcity);
        $('html,body').animate({scrollTop:'3em'},"slow");
        setTimeout(function(){
           $(circuitcity).css("position","absolute");
         },500);
        event.stopPropagation();
     })
     
    /*底部搜索按钮*/
    $('.Zone-three').on('click','.Zone-li li',function(event){
    	$('input[name=line_product_type]').val($(this).attr('line_product_type'));
    	$('.Zone-one,.Zone-twe,.Zone-three').hide();
    	ajaxGetLineList();
    	glidehide();
    	event.stopPropagation();
    })
    $('.Zone-twe').on('click','.Zone-li li',function(event){
        $('input[name=travel_day]').val($(this).attr('travel_day'));
        $('.Zone-one,.Zone-twe,.Zone-three').hide();
        ajaxGetLineList();
        glidehide();
    	event.stopPropagation();
    })
    $('.Zone-one').on('click','.Zone-li li',function(event){
    	$('input[name=price_range]').val($(this).attr('price_range'));
    	$('.Zone-one,.Zone-twe,.Zone-three').hide();
    	ajaxGetLineList();
        glidehide();
    	event.stopPropagation();
    })
    
    /* 出行人数增加*/
    $('.adult-right').on('click','.adultadd',function(event){
    	
        if( $('.price-xq .line_stock').text().length == 0){
        	getValidateEffect('请选择出行日期');
   		    return ;
        }
        var line_stock = parseInt( $('.price-xq .line_stock').text() );
    	var totalNum = parseInt($(".disabled-test").attr("value")) + parseInt($(".disabled-twe").attr("value"));
    	if(( totalNum < line_stock )&&( line_stock !=0 )){
    		var adult_input = $(this).parents(".adult-right").find("input");
            adult_input.attr("value",parseInt($(adult_input).attr("value"))+1);
            price();
    	}else{
    		getValidateEffect('库存数量不足');
    	}
 	    if( $(this).parents(".adult-right").hasClass("adult-cren") ){
 	       $(".cr-submit").attr("value",$(adult_input).attr("value"));
 	    }else{
 	       $(".ert-submit").attr("value",$(adult_input).attr("value"));
 	    }
 	    event.stopPropagation();   
    })
    
    $('.circuittest-order-botton').on('click','a.circuittest-order-alert',function(event){
         $('#my-confirm').modal({
            relatedTarget: this,
            onConfirm: function(options) {
                 location.href=$(".circuittest-order-alert").attr('href');
            },
            onCancel: function() {
            }
          });
            event.stopPropagation();   
    });

     /* 出行人数减少*/
    $('.adult-right').on('click','.adultreduce',function(event){
    	
        var adult_input = $(this).parents(".adult-right").find("input");
        if( $(this).parents(".adult-right").hasClass("adult-cren") ){
             if(parseInt($(adult_input).attr("value"))-1>0){
                adult_input.attr("value",parseInt($(adult_input).attr("value"))-1);
                $(".cr-submit").attr("value",$(adult_input).attr("value"));
              }
        }else{
 	         if( parseInt($(adult_input).attr("value"))-1>=0 ) {
 	            adult_input.attr("value",parseInt($(adult_input).attr("value"))-1);
 	            $(".ert-submit").attr("value",$(adult_input).attr("value"));
 	         }
        }
        price();
        event.stopPropagation();
    })
  
	 
	$('.circuittest-complain').on('click','.circuittest-radio',function(event){
        $(this).siblings('input[type="radio"]').attr('checked',true)
		$(".circuittest-radio").removeClass("active");
        $(this).addClass("active");
        event.stopPropagation();
    })
    
    /* 着想支付验证*/
    $(".balance-pay-form").validate({
	  	rules:{
	   	    password:{
	   		     required:true,
	   		     minlength:6,
	   		     remote:hostUrl()+'line/payment/checkPayPassword'
	       	   }
	    },
	    messages:{
	   	    password:{
	   		     required:'必填',
	   		     minlength:'密码最少为六位',
	   		     remote:'密码错误'
	       	   }
	    },
	    errorPlacement:function(error,element) { 
	    	var placement = $(element.parents(".circuitchild-password").find('.circuitchild-alert')); 
	        placement.text('');
	        error.appendTo(placement); 
	    },
	    success: function(element) {
	    	var placement = $(element.parents(".circuitchild-password").find('.circuitchild-alert'));
	    	placement.text('');
	    },
	    submitHandler:function(form){
	    	form.submit();
	        $('button[type=submit]').text('正在提交').attr('disabled',true);
	      
	    }
	});
    
    /*首页关键词城市刷新*/
    $('.circuittest-nav-ul').on('click','li',function(){
    	 $(this).addClass('am-active');
    	 $(this).siblings('li').removeClass('am-active');
    	 var city = $('.circuitheader-cs').text();
    	 city = city.replace('市/ig','');
    	 var travel_type_id = $(this).attr('travel-type-id');
    	 ajaxGetDhSearch(city,travel_type_id);
    })
    
    /*首页关键词搜索*/
    $('.dh-form').submit(function(event){
    	 var address=$('input[name="address"]').val();
    	 if(address.length == 0){
    		 return false;
    	 }
    	 var city = $('.circuitheader-cs').text();
    	 var start_address = city.replace(/市/,'');
         location.href=hostUrl()+'line/account/view?address='+address+'&start_address='+start_address;
    	 event.preventDefault();
    	 return false;
    })
    /*首页城市搜索 --替换城市*/
    $('.city-form').submit(function(event){
    	
    	var start_address = $.trim($('input[name=start_address]').val());
    	if( start_address.length==0 ){
    	    return false;
    	}
    	var city = start_address.replace(/市/g,'');
    	glidehide();
    	$('.circuitheader-cs').text(city);
    	event.preventDefault();
   	    return false;
    })
    /*日期表单提交时候*/
    $('.productdate-form').submit(function(event){
    	 $('button[type="submit"]').text('提交中').attr('disabled',true);
         return true;
    })
    /*日期表单提交时候 当页面刷新的时候*/
    $('.productdate-form button[type="submit"]').text('下一步').attr('disabled',false);
    
    /*订单填写页面*/
    $('.footer-display button[type=button]').click(function(event){
    	
 	    var chinese = /^[\u4e00-\u9fa5]+$/;
        var mobile = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
        var chkemail = /^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/; 
        var user_name = $.trim($('input[name="user_name"]').val());
        var telephone = $.trim($('input[name="telephone"]').val());
        var email = $.trim($('input[name="email"]').val()); 
        if( user_name.length==0 || telephone.length==0){
       	 getValidateEffect('请填订单信息');
   		 return false;
        }
        if(!chinese.test(user_name)){
       	 getValidateEffect('姓名请填中文');
   		 return false;
        }
        if(!mobile.test(telephone)){
       	 getValidateEffect('请填正确的电话格式');
   		 return false;
        }
		 if(　email.length !=0 &&　!(chkemail.test(email))){
			 getValidateEffect('请填正确的邮箱格式');
   		 return false;
		 }
		 $('.order-user-information').hide();
	     $('.order-guest-information').show();
 	     $('html,body').animate({scrollTop:'3em'},"normal");
		 event.stopPropagation();
		 return true;
    })
     /*订单填写页面--首页隐藏*/
    $('.order-guest-information').on('click','.order-guest-back',function(event){
	       $('.order-user-information').show();
	       $('.order-guest-information').hide();
	})
	/*订单填写页面--支付方法点击*/
	$('.pay-method').on('click','.am-modal-footer',function(event){
      var pay_type =  $(this).children('.pay-type').attr('pay-type') ;
      var pay_type_method = $(this).children('.pay-type').text();
      $(this).parents('.order-user-information').find('.pay-method-type').text(pay_type_method);
      $(this).parents('.order-user-information').find('input[name="pay_style_type"]').val(pay_type);
      $(this).parents('.order-user-information').find('.pay-scale-type').text('100%');
      $(this).parents('.order-user-information').find('input[name="pay_style_scale"]').val('1');
      if(pay_type == '-9999'){
          $('.circuittest-order-too .pay-scale-none').show();
      }else{
     	 ajaxNewPage(false);
     	 $('.circuittest-order-too .pay-scale-none').hide();
       }
   })
   /*订单填写页面--支付比例点击*/
   $('.pay-scale').on('click','.am-modal-footer',function(event){
 	  var pay_scale_value =  $(this).children('.pay-scale-value').attr('pay-scale-value') ;
      var pay_scale = $(this).children('.pay-scale-value').text();
      $(this).parents('.order-user-information').find('.pay-scale-type').text(pay_scale);
      $(this).parents('.order-user-information').find('input[name="pay_style_scale"]').val(pay_scale_value);
      ajaxNewPage(false);
   })
   
   /*订单填写页面--单房差点击*/
  $('.pay-home-price').on('click','.am-modal-footer',function(event){
     var home_price_value = $(this).children('.home-price-type').attr('home-price');
     var home_price = $(this).children('.home-price-type').text();
     $(this).parents('.order-user-information').find('.pay-home-price-type').text(home_price);
     $(this).parents('.order-user-information').find('input[name="home_price"]').val(home_price_value);
     ajaxNewPage(false);
  })
   /*订单填写页面--验证+ +ajax+ 提交页面*/
  $('.supplier-checkout').validate({
	  submitHandler: function(f) {
		  $.ajax({
              type:'post',
              async:false,
              dataType:'json',
              url:hostUrl()+'line/linecheckout/submitOrder',
              data: $('.visitor-checkout,.supplier-checkout').serialize(),
              beforeSend: function() {
                  $('.footer-accounts button[type=submit]').text('加载中').attr('disabled', true);
              },
              complete: function() {
                  //加载完成后
              },
              success: function(json) {
                  if (json.status) {
                      window.location.href = hostUrl()+json.messages;
                  } else {
                	  $('.footer-accounts button[type=submit]').text('重新提交').attr('disabled',false);
                      getValidateEffect(json.messages);
                  }
              }
          });
          return false;
	  }
  });
  /*页面刷新的时候 -- 底部订单提交按钮回复原样*/
  $('.footer-accounts button[type=submit]').text('下一步').attr('disabled',false); 
});
/* 滚动刷新翻页*/
var page = 2;
$(window).scroll(function(){

	var scrollTop = parseFloat($(this).scrollTop());
	var scrollHeight = parseFloat($(document).height());
	var windowHeight = parseFloat($(this).height());
	var count = $.trim($('input[name=count]').val());
	var start_address = $.trim($('input[name=start_address]').val());
	var line_travel_type = $.trim($('input[name=line_travel_type]').val());
	var address = $.trim($('input[name=address]').val());
	var price_range = $.trim($('input[name=price_range]').val());
	var travel_day = $.trim($('input[name=travel_day]').val());
	var line_product_type = $.trim($('input[name=line_product_type]').val());
	var url = hostUrl()+'line/account/view';
	if( page <= count ){
		if( scrollTop + windowHeight >= scrollHeight ) {
	    	   $.ajax({
	    			type:'get',
	    			url :url,
	    			data:{'page':page,'start_address':start_address,'address':address,'line_travel_type':line_travel_type,'price_range':price_range,'travel_days':travel_day,'line_product_type':line_product_type},
	    		    dataType:'json',
	    		    async: false,
	    		    beforeSend:function(XMLHttpRequest){ hideShowConvert()},
	    		    success:function(json){
	    		    	if(json.status){
	    		    		$('.circuittest-like .loading').addClass('none-winadd');
	    		    		$('.list li').last().after(json.content);
	    		    		$("img.lazy").lazyload({}); 
	    		    		page++;
	    		    	}else{
	    		    		showHideConvert();
	    		    	}
	    		    }
	    		  })
	        }
	}else{
    	showHideConvert();
	}
	if(scrollTop > 200){
		slideToUpShow();
	}else{
		slideToUpHide();
	}
})

function showHideConvert(){
	$('.circuittest-like .no-loading').removeClass('none-winadd');
    $('.circuittest-like .loading').addClass('none-winadd');
}

function hideShowConvert(){
	$('.circuittest-like .no-loading').addClass('none-winadd');
    $('.circuittest-like .loading').removeClass('none-winadd');
}

function glideShow(classname){
    $(classname).show().animate({left:"0px"},"slow");
    setTimeout(function(){
        $('.circuitheader,.circuittest,.circuitfooter').hide();
    },500);
}

function glidehide(){
    $('.circuitheader,.circuittest,.circuitfooter').show();
    $('.circuitcity,.circuitnavigate').animate({left:"100%"},"slow");
    setTimeout(function(){
        $('.circuitcity,.circuitnavigate').hide();
    },500);
}

function getValidateEffect($errorMessage){
	
	$('.modal-outstanding .error-message').text($errorMessage);
	$('#alert').modal({
        relatedTarget:this
    });
}
function minHeight(){
    var minheight=document.body.scrollHeight;
    $(".circuittest-nav-ul").css("minHeight",minheight+"px");
}

function slideToUpShow(){
	$('.return-yuan').show();
}

function slideToUpHide(){
	$('.return-yuan').hide();
}

function price(){
	  var price=($(".crenprice").html()*$(".disabled-test").attr("value")+$(".crenpriert").html()*$(".disabled-twe").attr("value")).toFixed(2);
	  $(".price-test").attr("value",price);
}

function ajaxGetLineList(){
   
	var start_address = $.trim($('input[name=start_address]').val());
	var line_travel_type = $.trim($('input[name=line_travel_type]').val());
	var address = $.trim($('input[name=address]').val());
	var price_range = $.trim($('input[name=price_range]').val());
	var travel_day = $.trim($('input[name=travel_day]').val());
	var line_product_type = $.trim($('input[name=line_product_type]').val());
	var url = hostUrl()+'line/account/ajaxGetLineList';
	$.ajax({
		type:'get',
		url :url,
		data:{'start_address':start_address,'address':address,'line_travel_type':line_travel_type,'price_range':price_range,'travel_days':travel_day,'line_product_type':line_product_type},
	    dataType:'json',
	    async: false,
	    beforeSend:function(XMLHttpRequest){ hideShowConvert()},
	    success:function(json){
	    	if(json.status){
	    		$('.circuittest-like .loading').addClass('none-winadd');
	    		$('input[name=count]').val(json.count);
	    		$('.line-list ul').html(json.content);
	    		$("img.lazy").lazyload({}); 
	    		page = 2;
	    	}else{
	    		$('.line-list ul').html(json.nocontent);
	    	}
	    }
	})
}

function validate_zhifubao(){
    $('input[name="pay_bank"]').val('1');
    $('form').submit();
}

function validate_weixin(){
    $('input[name="pay_bank"]').val('201');
    $('form').submit();
}

function validate_ye(){
    $('input[name="pay_bank"]').val('99');
    $('form').submit();
}

function focusAjaxGetDhSearch(){
	
	var city = $('.circuitheader-cs').text();
    var travel_type_id = '1';
	city = city.replace(/市/ig,'');
	$('.circuittest-nav-ul  li:first-child').addClass('am-active');
	$('.circuittest-nav-ul  li:first-child').siblings().removeClass('am-active');
	ajaxGetDhSearch(city,travel_type_id);
}

function ajaxGetDhSearch(city,type){
	
	var url = hostUrl()+'line/account/ajaxGetDhSearch';
	$.ajax({
		 type:'get',
		 url :url,
		 data:{'city':city,'type':type},
		 dataType:'json',
		 async: false,
		 beforeSend:function(XMLHttpRequest){},
		 complete:function(){},
		 success:function(json){
	    	if(json.status){
	    		$('.ajax-get-dh-search').html(json.content);
	    	}
	    }
	})
}

function ajaxNewPage(is_visitors){

    $.ajax({
        type: 'post',
        async: false,
        dataType:'json',
        url:hostUrl()+'line/linecheckout/ajaxCheckoutSupplierPrice',
        data:$('form').serialize(),
        success:function(json) {
            if(json.status) {
                $('.footer-accounts .saleser-order-price-html').html(json.orderPriceHtml);
                if (is_visitors) {
                    $('.footer-accounts .saleser-order-price-html').html(json.supplierVisitor);
                }
            }
        }
     });
}

/**
 * 获取host域名
 * @returns
 */
function hostUrl(){
	return location.protocol+'//'+location.host+'/';
}


